﻿#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: TournamentsController.cs
//

#endregion

using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Novagoal.Logic;
using Novagoal.Models;
using Novagoal.Website.Models.Forms;
using Novagoal.Website.Models.Views;
using Novagoal.Website.WebApp.Routing;

namespace Novagoal.Website.WebApp.Controllers
{
    public class TournamentsController : ListController
    {
        #region PROPERTIES

        private readonly TournamentsLogic _tournamentsLogic;

        #endregion

        #region CONSTANTS
        
        private const string LOCAL_FLAG_PATH = "~/Content/Trophies";
        #endregion

        #region PUBLIC METHODS

        public TournamentsController(UsersLogic usersLogic, TournamentsLogic tournamentsLogic)
            : base(usersLogic)
        {
            _tournamentsLogic = tournamentsLogic;
        }

        [HttpGet]
        public ActionResult Index()
        {
            var tournaments = _tournamentsLogic.GetTournaments(Request.Params, SortField, SortDirection, PageNumber, PageSize);

            var indexViewModel = new IndexViewModel
            {
                List = tournaments,
                RouteValues = tournaments.GetRouteValues(PageSize)
            };
            return View("Index", indexViewModel);
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return Redirect(collection);
        }

        [HttpGet]
        public ActionResult Add()
        {
            var model = new TournamentsViewModel
                            {
                                Tournament = new Tournament()
                            };
            return View("Details", model);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var model = new TournamentsViewModel
            {
                Tournament = _tournamentsLogic.GetTournament(id)
            };
            return View("Details", model);
        }

        public ActionResult Save(TeamFormModel formModel)
        {
            //var modelView = CreateModelFromForm(formModel);
            //if(ModelState.IsValid)
            //{
            //    modelView.Team.FlagPath = SaveFlag(formModel.Flag, modelView.Team.Name);
            //    modelView.Team.Id = _teamsLogic.SaveTeam(modelView.Team);
            //    modelView.SuccessMessage = "Team saved successfully";
            //}
            //return View("Details", modelView);
            throw new NotImplementedException("To do");
        }

        #endregion

        #region PRIVATE METHODS

        private string SaveFlag(HttpPostedFileBase flag, string teamName)
        {
            var extension = flag.FileName.Split('.').LastOrDefault();
            var flagName = string.Format("{0}.{1}", teamName, extension);
            var path = Path.Combine(Server.MapPath(LOCAL_FLAG_PATH), flagName);

            var fileinfo = new FileInfo(path);
            if(fileinfo.Exists)
            {
                fileinfo.Delete();
            }

            flag.SaveAs(path);

            return string.Format("{0}/{1}",LOCAL_FLAG_PATH, flagName);
        }

        private TeamsViewModel CreateModelFromForm(TeamFormModel formModel)
        {
            var currentUser = GetUserInformation();
            var ms = new MemoryStream();
            if(formModel.Flag != null) formModel.Flag.InputStream.CopyTo(ms);
            var viewModel = new TeamsViewModel
                                {
                                    Flag = ms.ToArray(),
                                    Team = new Team
                                               {
                                                   Id = formModel.TeamId,
                                                   Name = formModel.Name,
                                                   UpdatedBy = currentUser.Person.Id,
                                               }
                                };
            return viewModel;
        }

        #endregion
    }
}
